package com.androidhive.dashboard;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.androidhive.dashboard.Voting_Activity.GetTeamNames;

import votd_notification.AlarmManagerBroadcastReceiver;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import androidhive.dashboard.R;
import database.helpers.VOTD_Table;

public class Home_Activity extends ActionBarActivity {
	private AlarmManagerBroadcastReceiver alarm;
	VOTD_Table votdTab;
    private DashboardLayout dash;
	// This is a handle so that we can call methods on our service

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard_layout);
		
		
		dash = (DashboardLayout) findViewById(R.id.dashlay);
	    Drawable d = new BitmapDrawable(getResources(),decodeSampledBitmapFromResource(getResources(), R.drawable.ofv, 300, 300)); 
	    dash.setBackground(d);
		customizeactionBar();
		
//		setVOTD();
		

		/**
		 * Changing appearance of ActionBar
		 * */
		// http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=appcompat&theme=light&actionbarstyle=solid&texture=1&hairline=0&backColor=9e2e2e%2C100&secondaryColor=e4e4e4%2C100&tabColor=33B5E5%2C100&tertiaryColor=F2F2F2%2C100&accentColor=33B5E5%2C100&cabBackColor=FFFFFF%2C100&cabHighlightColor=33B5E5%2C100

		/**
		 * Creating all buttons instances
		 * */
		// Dashboard News feed button
		Button btn_whosJesus = (Button) findViewById(R.id.btn_whos_Jesus);

		// Dashboard Friends button
		Button btn_discipleship = (Button) findViewById(R.id.btn_discipleship);

		// Dashboard Messages button
		Button btn_testimony = (Button) findViewById(R.id.btn_testimony);

		// Dashboard Places button
		Button btn_more_conent = (Button) findViewById(R.id.btn_more_content);

		// Dashboard Events button
		Button btn_faq = (Button) findViewById(R.id.btn_faq);

		// Dashboard Photos button
		Button btn_contact_us = (Button) findViewById(R.id.btn_contact_us);

		/**
		 * Handling all button click events
		 * */

		// Listening to News Feed button click
		btn_whosJesus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching News Feed Screen
				Intent i = new Intent(getApplicationContext(),
						Competition_Activity.class);
				startActivity(i);
			}
		});

		// Listening Friends button click
		btn_discipleship.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {

				Intent i = new Intent(getApplicationContext(),
						Team_Activity.class);
				startActivity(i);
			}
		});

		// Listening Messages button click
		btn_testimony.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching News Feed Screen
				Intent i = new Intent(getApplicationContext(),
						Testimony_Activity.class);
				startActivity(i);
			}
		});

		// Listening to Places button click
		btn_more_conent.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching News Feed Screen
				Intent i = new Intent(getApplicationContext(),
						MoreContent_Activity.class);
				startActivity(i);
			}
		});

		// Listening to Events button click
		btn_faq.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching News Feed Screen
				Intent i = new Intent(getApplicationContext(),
						Voting_Activity.class);
				startActivity(i);
			}
		});

		// Listening to Photos button click
		btn_contact_us.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching News Feed Screen
				Intent i = new Intent(getApplicationContext(),
						ContactUs_Activity.class);
				startActivity(i);
			}
		});
	}

	private void customizeactionBar() {
		// TODO Auto-generated method stub
		ActionBar ab = getSupportActionBar();
		// Resources res = getResources();
		// ab.setBackgroundDrawable(res.getDrawable(R.drawable.background));
		// ab.setTitle("Zvaita");
		GradientDrawable gd = new GradientDrawable(
				GradientDrawable.Orientation.TR_BL, new int[] {
						Color.parseColor("#090606"),
						Color.parseColor("#FA0202") });
		ab.setBackgroundDrawable(gd);
	}

	private void setVOTD() {
		// TODO Auto-generated method stub
		alarm = new AlarmManagerBroadcastReceiver();
		alarm.SetAlarm(this);
		votdTab= new VOTD_Table(this);
		if (votdTab.isEmpty())
		{
		readTextFile("votd.txt");
		}
		
	}

	private void readTextFile(String fileName) {
		InputStream is = this.getResources().openRawResource(R.raw.votd);
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer// size

		try {
			String ref_scripture,scripture;
			while (true) {
				ref_scripture = br.readLine();	
				scripture = br.readLine();
				// readLine() returns null if no more lines in the file
				if (ref_scripture == null||scripture == null)
					break;
				else {
					votdTab.addVOTD("Today", ref_scripture, scripture,"false");
					
					// temp.append("\n" + "    " + test);
				}
			}
			isr.close();
			is.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
	        int reqWidth, int reqHeight) {

	    // First decode with inJustDecodeBounds=true to check dimensions
	    final BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeResource(res, resId, options);

	    // Calculate inSampleSize
	    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;
	    return BitmapFactory.decodeResource(res, resId, options);
	}
	
	public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) 
        {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

}